<template>
  <div class="home" @click="onSubmit">
    <div>
      <img src="../../../assets/img/img_5.jpeg">
      <img src="../../../assets/img/img_58.png">
    </div>
    <div class="content">
      <van-cell-group inset style="background-color: transparent;">
        <van-field v-model="phoneNumber" name="手机号"  placeholder="请输入手机号"  ref="inputNumber" maxlength="11"
          autocomplete="off" type="number" @update:model-value="onChange"  size="large"/>
      </van-cell-group>
    </div>
    <div class="footer">
        <h3 style="text-algin:center">联系我们</h3>
       
        <p>客服和投诉通道：<a class="tel" href="tel:4009980208"
          data-v-a9f2f654="" style="color:#fff">4009980208</a></p>
        <p>客服服务时间：9:00-18:00</p>
        <p>退款方式：关注[月得权益]公众号</p>
        <p class="company">海南沐沐潺潺网络科技有限公司</p>
    </div>
    <div class="height"></div>
   
  </div>
</template>
<script>
/* eslint-disable*/
/* eslint-disable*/
import { ref, reactive, getCurrentInstance, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showToast, showConfirmDialog } from 'vant'
import api from '../api/index'
import { Base64 } from 'js-base64';
import Footer from './components/footer.vue';
const urlController = (url) => {
  var _url = url.split('?')[1]
  if (!_url) {
    return {}
  }
  var windowHref = _url.split('&')
  var obj = {}
  for (var i = 0; i < windowHref.length; i++) {
    var arr = windowHref[i].split('=')
    obj[arr[0]] = arr[1]
  }
  return obj
}

export default {
  name: 'Home',
  components: {
    // HelloWorld
    Footer
  },
  setup() {
    const router = useRouter()
    const phone = urlController(location.href)?.phone && Base64.decode(urlController(location.href)?.phone) || ''
    const codeRe=  urlController(location.href)?.code || ''
    const bxmid =  urlController(location.href)?.bxm_id  || '' // 变现猫
    const userkey =  urlController(location.href)?.userkey  || '' // xmod
    const bc_tag = urlController(location.href)?.bc_tag  || '' // 百川
    const nadkey =  urlController(location.href)?.nadkey  || '' // 秦禾
    const plid = urlController(location.href)?.plid  || 0
    const operation = urlController(location.href)?.operation  || ''
    const phoneNumber = ref(phone)
    const checked = ref(1)
    const amount = 50
    const checkedPay = ref('1')
    const reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    let currentInstance = ''

    onMounted(() => {
      currentInstance = getCurrentInstance()
    })
    const fnClickRules = () => {
      // show.value = true
    }
    const fnclickPrivacy = () => {
      router.push('/privacyAgreement')
    }
    const fnclickAgreen = () => {
      router.push('/agree')
    }
    const fnClickPe = () => {
      window.open('http://kf.holyn.cn/mobile/index?code=sIEyy19fwiK8ELYdy3FkUkSNN1sLpcZ5jKi4MYA3XOG1NLxUpNmNLWcmz12qHQoQlj2s4iXw%2FzjgvtmomDR0VshLNZ5BAxBLxRoBsOnah8%2FS7vKKZuG9lF1RoudmuJT4VA')
    }
    // 提交按钮
    const onSubmit = () => {
      if (!phoneNumber.value) {
        showToast('请输入手机号')
        currentInstance.proxy.$refs.inputNumber.focus()
        return
      }
      if (!reg_tel.test(phoneNumber.value)) {
        showToast('请输入正确手机号')
        return
      }
      getPaw()
    }
    const onChange = () => {
      if(phoneNumber.value.length === 11){
        currentInstance.proxy.$refs.inputNumber.blur()
      }
    }
    const getPaw = () => {
      api.get('getOutTradeNo', { phone: phoneNumber.value }).then(res => {
        if (res.code === 200) {
          const { count, orderNo } = res.data
          if (!count) {
            showToast('您的购买已到达上限')
            return
          }
            getWxlink(orderNo)
        } else {
          showToast(res.message)
        }
      })
    }
    const showConfirm = (outTradeNo) => {
      showConfirmDialog({
        title: '支付确认',
        confirmButtonText: '已支付完成',
        cancelButtonText: '未支付',
        message:
          '您发起的订单支付是否已经支付完成。'
      })
        .then(() => {
          router.push({
            path: '/result',
            query: {
              outTradeNo: outTradeNo,
              amount: amount,
              phoneNumber: phoneNumber.value
            }
          })
        })
        .catch(() => {
        })
    }
    const getWxlink = (outTradeNo) => {
      const params =montageParams(outTradeNo)
      api.get('getWxLink', { ...params }).then(res => {
        if (res.code * 1 === 200) {
          location.href = res.data
        } else {
          showToast(res.message)
        }
      })
    }
    // 整理参数
    const montageParams=(outTradeNo)=>{
      let code ='',advert='cat',other = 0 //唯一码 媒体类型 特殊字段
      if(!codeRe){
        if(bxmid){ 
          code =bxmid
          advert  ='cat'
        }else if(userkey){
          code =userkey
          advert  ='xmob'
          other=plid
        } else if(bc_tag){
          code =bc_tag
          advert  ='baichuan'
        }else{
          code =nadkey
          advert  ='nuohe'
        }
      }else{
        code = codeRe
        advert = urlController(location.href)?.advert 
        other = urlController(location.href)?.other || 0
        // operation = urlController(location.href)?.operation
      }
      const params = {
        outTradeNo: outTradeNo,
        amount: amount,
        phoneNumber: phoneNumber.value,
        code:code,
        advert:advert,
        other:other,
        operation:operation
      }
      localStorage.setItem("code", code);
      localStorage.setItem("advert", advert || '');
      localStorage.setItem("other", other);
      localStorage.setItem("phoneNumber", phoneNumber.value);
      localStorage.setItem("operation", operation);
      localStorage.setItem('route',location.hash)
      return params
    }
    const success = () => {
      location.href ='https://www.qhdyywlkj.com/download/app.html'
    }
    return {
      phoneNumber,
      checked,
      amount,
      fnClickRules,
      fnclickPrivacy,
      fnclickAgreen,
      fnClickPe,
      onSubmit,
      getPaw,
      showConfirm,
      getWxlink,
      onChange,
      checkedPay,
      montageParams,
      success
    }
  }
}
</script>
<style lang="scss" scoped>
.home {
  position: relative;
  font-size: .14rem;
  color: #000;
  background-color: #3961e2;
  img{
    display: block;
    width:100%;
  }
  .content{
    position: absolute;
    left: 0%;
    top: 69vw;
    border-radius: 14.9vw;
    width: 100%;
    height: 14.9vw;
    background-color: transparent;
    font-size: 7vw;
    padding: 0 4vw;
    box-sizing: border-box;
    border: transparent 1px solid;
    /deep/.van-cell{
      background:transparent !important
    }
  }
  .footer{
    width: 60%;
    margin: 0 auto;
    margin-top:0.3rem;
    font-weight: bold;
    padding: 10px;
    color: white;
    text-align: left;
    border: 2px solid white;
    border-radius: 10px;
    font-size: .08rem;
    line-height: 20px;
    h3{
      text-align: center;
      font-size: 0.1rem;
    }
  }
  .height{
    height:.3rem;
  }
  .company{
    text-align: center;
  }
}

</style>
